import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.TransactionException;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.spec.InvalidKeySpecException;

/**
 * Created by xuguangyuansh on 2018/8/23.
 */
public class FabricManager extends BaseManager {

    private ChaincodeManager abericChaincodeManager;

    private static class FabricManagerFactory {
        private static FabricManager instance = new FabricManager();
    }

    public static FabricManager obtain() {
        return FabricManagerFactory.instance;
    }

    private FabricManager() {
        try {
            abericChaincodeManager = obtainChaincodeManager();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (InvalidArgumentException e) {
            e.printStackTrace();
        } catch (TransactionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (CryptoException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public ChaincodeManager getChaincodeManager() {
        return abericChaincodeManager;
    }

    private ChaincodeManager obtainChaincodeManager() throws IOException, NoSuchAlgorithmException, InvocationTargetException,
            InstantiationException, InvalidKeySpecException, InvalidArgumentException, TransactionException,
            IllegalAccessException, CryptoException, NoSuchProviderException, NoSuchMethodException, ClassNotFoundException {
        FabricConfig fabric = new FabricConfig();
        ChaincodeManager chaincodeManager = null;
        chaincodeManager = fabric
                .setUser("Admin")
                .setChannelArtifactsPath(getChannelArtifactsPath("aberic"))
                .setCryptoConfigPath(getCryptoConfigPath("aberic"))
                .setPeers("Org1", "Org1MSP", "org1.example.com")
                .addPeer("peer0.org1.example.com", "peer0.org1.example.com", "grpc://192.168.24.251:7051", "grpc://192.168.24.24:7053", "http://192.168.24.251:7054")
                .setOrderers("example.com")
                .addOrderer("orderer0.example.com", "grpc://192.168.24.251:7050")
                .setChaincode("mychannel", "marble1", "github.com/hyperledger/fabric/aberic/chaincode/go/marbles02", "1.0")
                .getChaincodeManager();
        return chaincodeManager;
    }
}




















































